﻿using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Areas.Veggies.Controllers
{
    public class FruitAutocompleteController : Controller
    {
        private readonly IRepo<Fruit> r;

        public FruitAutocompleteController(IRepo<Fruit> r)
        {
            this.r = r;
        }

        public JsonResult Search(string searchText, int maxResults, int? parent)
        {
            return Json(r.GetAll().Where(o =>
                                         o.Name.ToLower().Contains(searchText.ToLower()))
                            .Select(p => new IdTextItem { Text = p.Name, Id = p.Id })
                            .Take(maxResults));
        }
    }
}